package com.hnqcgczy.shop.dao.impl;

import com.hnqcgczy.shop.dao.IProductCategoryDao;
import com.hnqcgczy.shop.db.MyTransaction;
import com.hnqcgczy.shop.vo.TProduct;
import com.hnqcgczy.shop.vo.TProductCategory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class IProductCategoryDaoimpl implements IProductCategoryDao {
    MyTransaction myTransaction=new MyTransaction(true);

    public IProductCategoryDaoimpl(MyTransaction myTransaction) {
        this.myTransaction = myTransaction;
    }

    @Override
    public List<TProductCategory> listAll() throws SQLException {
        Connection conn = myTransaction.getConn();
        //PreparedStatement向数据库发送sql语句
        String sql = "select *from  t_product where tp_id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        //执行sql语句
        ResultSet rs = ps.executeQuery();
        List<TProductCategory>list=new ArrayList<>();
        //将查询到的虚拟表中的多条数据放入集合中
        while (rs.next()){
            TProductCategory tProductCategory=new TProductCategory();
            tProductCategory.setTpcId(rs.getInt(1));
            tProductCategory.setTpcName(rs.getString(2));
            tProductCategory.setTpcParentId(rs.getInt(3));
            //每循环一次，将实体类对象放入集合
            list.add(tProductCategory);
        }
        //释放资源
        rs.close();
        ps.close();
        myTransaction.closeconn();
        return list;
    }

    @Override
    public List<TProductCategory> querAny(int page, int pageSize) {
        return null;
    }

    @Override
    public TProductCategory querByID(int tpc_id) throws SQLException {
        Connection conn = myTransaction.getConn();
        //PreparedStatement向数据库发送sql语句
        String sql = "select *from  t_product where tp_id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,tpc_id);
        //执行sql语句
        ResultSet rs = ps.executeQuery();
        TProductCategory tProductCategory=null;
        //将查询到的虚拟表中的多条数据放入集合中
        while (rs.next()){
            tProductCategory=new TProductCategory();
            tProductCategory.setTpcId(rs.getInt(1));
            tProductCategory.setTpcName(rs.getString(2));
            tProductCategory.setTpcParentId(rs.getInt(3));
        }
        //释放资源
        rs.close();
        ps.close();
        myTransaction.closeconn();
        return tProductCategory;

    }

    @Override
    public boolean deleteById(int tpc_id) throws SQLException {
        return false;
    }

    @Override
    public boolean updateById(int tpc_id) throws SQLException {

        return false;

    }

    @Override
    public boolean insert(TProductCategory tProductCategory) throws SQLException {
        return false;
    }
}
